<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单编号" prop="orderNumber">
        <el-input v-model="queryParams.orderNumber" placeholder="请输入工单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务专业" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务专业" clearable>
          <el-option v-for="dict in dict.type.insp_task_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="区段" prop="section">
        <el-select v-model="queryParams.section" placeholder="请选择区段" clearable>
          <el-option label="K123+100-K124+100" value="K123+100-K124+100" />
          <el-option label="K223+100-K224+100" value="K223+100-K224+100" />
          <el-option label="K323+100-K324+100" value="K323+100-K324+100" />
          <el-option label="K423+100-K424+100" value="K423+100-K424+100" />
          <el-option label="K523+100-K524+100" value="K523+100-K524+100" />
          <el-option label="K623+100-K624+100" value="K623+100-K624+100" />
          <el-option label="K723+100-K724+100" value="K723+100-K724+100" />
          <el-option label="K300+200-K324+200" value="K300+200-K324+200" />
          <el-option label="K600+200-K624+200" value="K600+200-K624+200" />
        </el-select>
      </el-form-item>
      <el-form-item label="作业平台" prop="platform">
        <el-select v-model="queryParams.platform" placeholder="请选择平台" clearable>
          <el-option v-for="dict in dict.type.insp_task_platform" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="巡检任务" prop="task">
        <el-input v-model="queryParams.task" placeholder="请输入巡检任务" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="作业规范" prop="specifications">
        <el-input v-model="queryParams.specifications" placeholder="请输入作业规范" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始时间" prop="planStartTime">
        <el-date-picker clearable v-model="queryParams.planStartTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择计划开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="planEndTime">
        <el-date-picker clearable v-model="queryParams.planEndTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择计划结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="作业内容" prop="planContent">
        <el-input v-model="queryParams.planContent" placeholder="请输入作业内容" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['inspection:plan:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['inspection:plan:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['inspection:plan:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['inspection:plan:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-s-promotion" size="mini" :disabled="single" @click="handleRelease"
          v-hasPermi="['inspection:plan:export']">发布</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="工单编号" align="center" prop="orderNumber" />
      <el-table-column label="任务专业" align="center" prop="taskType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.insp_task_type" :value="scope.row.taskType" />
        </template>
      </el-table-column>
      <el-table-column label="区段" align="center" prop="section" />
      <el-table-column label="作业平台" align="center" prop="platform">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.insp_task_platform" :value="scope.row.platform" />
        </template>
      </el-table-column>
      <el-table-column label="巡检任务" align="center" prop="task" />
      <el-table-column label="作业规范" align="center" prop="specifications" />
      <el-table-column label="开始时间" align="center" prop="planStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="planEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作业内容" align="center" prop="planContent" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleCheck(scope.row)">查看工单</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['inspection:plan:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['inspection:plan:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改巡检计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="工单编号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入工单编号" />
        </el-form-item> -->
        <el-form-item label="任务专业" prop="taskType">
          <el-select v-model="form.taskType" placeholder="请选择任务专业">
            <el-option v-for="dict in dict.type.insp_task_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区段" prop="section">
          <el-select v-model="form.section" placeholder="请选择区段">
            <el-option label="K123+100-K124+100" value="K123+100-K124+100" />
            <el-option label="K223+100-K224+100" value="K223+100-K224+100" />
            <el-option label="K323+100-K324+100" value="K323+100-K324+100" />
            <el-option label="K423+100-K424+100" value="K423+100-K424+100" />
            <el-option label="K523+100-K524+100" value="K523+100-K524+100" />
            <el-option label="K623+100-K624+100" value="K623+100-K624+100" />
            <el-option label="K723+100-K724+100" value="K723+100-K724+100" />
            <el-option label="K300+200-K324+200" value="K300+200-K324+200" />
            <el-option label="K600+200-K624+200" value="K600+200-K624+200" />
          </el-select>
        </el-form-item>
        <el-form-item label="作业平台" prop="platform">
          <el-select v-model="form.platform" placeholder="请选择平台">
            <el-option v-for="dict in dict.type.insp_task_platform" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="巡检任务" prop="task">
          <el-input v-model="form.task" placeholder="请输入巡检任务" />
        </el-form-item>
        <el-form-item label="作业规范" prop="specifications">
          <el-input v-model="form.specifications" placeholder="请输入作业规范" />
        </el-form-item>
        <el-form-item label="开始时间" prop="planStartTime">
          <el-date-picker clearable v-model="form.planStartTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择计划开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="planEndTime">
          <el-date-picker clearable v-model="form.planEndTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择计划结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作业内容" prop="planContent">
          <el-input v-model="form.planContent" placeholder="请输入作业内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 弹窗 -->
    <el-dialog title="查看工单" :visible.sync="dialogVisible" width="50%">
      <div class="task-details">
        <!-- 表格内容 -->
        <div class="details-grid">
          <el-row class="row">
            <el-col :span="6" class="label">工单编号</el-col>
            <el-col :span="18" class="value">{{ form.orderNumber }}</el-col>
          </el-row>
          <el-row class="row">
            <el-col :span="6" class="label">任务专业</el-col>
            <el-col :span="18" class="value">
              <dict-tag :options="dict.type.insp_task_type" :value="form.taskType" />
            </el-col>
          </el-row>
          <el-row class="row">
            <el-col :span="6" class="label">巡检区段</el-col>
            <el-col :span="18" class="value">{{ form.section }}</el-col>
          </el-row>
          <el-row class="row">
            <el-col :span="6" class="label">作业平台</el-col>
            <el-col :span="18" class="value">
              <dict-tag :options="dict.type.insp_task_platform" :value="form.platform" />
            </el-col>
          </el-row>
          <el-row class="row">
            <el-col :span="6" class="label">巡检任务</el-col>
            <el-col :span="18" class="value">{{ form.task }}</el-col>
          </el-row>
          <el-row class="row">
            <el-col :span="6" class="label">作业规范</el-col>
            <el-col :span="18" class="value">{{ form.specifications }}</el-col>
          </el-row>
          <el-row class="row">
            <el-col :span="6" class="label">计划时间</el-col>
            <el-col :span="18" class="value">{{ form.planStartTime }}</el-col>
          </el-row>
          <el-row class="row">
            <el-col :span="6" class="label">作业内容</el-col>
            <el-col :span="18" class="value">{{ form.planContent }}</el-col>
          </el-row>
        </div>

        <!-- 地图展示 -->
        <div class="map-container">
          <img src="https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png"
            alt="任务地图" />
        </div>
      </div>

      <!-- 弹窗操作按钮 -->
      <template #footer>
        <el-button @click="dialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan, releasePlan } from "@/api/inspection/plan";

export default {
  name: "Plan",
  dicts: ['insp_task_type', 'insp_task_platform'],
  data() {
    return {
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        taskType: null,
        section: null,
        platform: null,
        task: null,
        specifications: null,
        planStartTime: null,
        planEndTime: null,
        planContent: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询巡检计划列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        taskType: null,
        section: null,
        platform: null,
        task: null,
        specifications: "GD2024.1 新朔铁路供电系统无人机巡检作业规范",
        planStartTime: null,
        planEndTime: null,
        planContent: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //查看工单
    handleCheck(row) {
      this.dialogVisible = true
      const id = row.id || this.ids
      getPlan(id).then(response => {
        this.form = response.data;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡检计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡检计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除巡检计划编号为"' + ids + '"的数据项？').then(function () {
        return delPlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('inspection/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    },
    /** 计划发布 **/
    handleRelease(row) {
      const id = row.id || this.ids[0];
      const data = {
        id: id,
      }
      this.$modal.confirm('是否确认发布巡检计划？').then(function () {
        return releasePlan(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("发布成功");
      }).catch(() => { });
    }
  }
};
</script>

<style scoped>
::v-deep .el-dialog__body {
  padding: 5px 20px;
}

::v-deep .el-dialog__header {
  background-color: rgb(0, 0, 255);
  color: white;

  .el-dialog__title {
    color: white;
  }

  .el-dialog__headerbtn .el-dialog__close {
    color: white;
  }
}

.task-details {
  display: flex;
  flex-direction: column;
}

/* 自定义网格样式 */
.details-grid {
  margin-bottom: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  overflow: hidden;
}

.details-grid .row {
  display: flex;
  border-bottom: 1px solid #ccc;
}

.details-grid .row:last-child {
  border-bottom: none;
}

.details-grid .label {
  background-color: #f4f6ff;
  font-weight: bold;
  padding: 10px;
  border-right: 1px solid #ccc;
}

.details-grid .value {
  padding: 10px;
}

/* 地图样式 */
.map-container {
  width: 100%;
  /* 设置容器宽度 */
  text-align: center;
  margin-top: 5px;
}

.map-container img {
  width: 100%;
  /* 设置图片宽度 */
  height: 20vw;
  /* 设置图片高度 */
  object-fit: cover;
  /* 保持图片比例 */
}
</style>